package com.xd.study.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.xd.study.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DGF on 2015/4/21.
 */
public class ListViewActivity extends Activity {

    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String,Object>> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        listView = (ListView) this.findViewById(R.id.listView);

//        ArrayAdapter<String> arrayAdapter;
//        String[] arr = {"imooc 1", "imooc 2", "imooc 3", "imooc 4"};
//        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
//        listView.setAdapter(arrayAdapter);

        data = new ArrayList<Map<String,Object>>();
        for (int i=0;i<10;i++){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("pic", R.drawable.toggle_button_off);
            map.put("txt", "慕课网"+i);
            data.add(map);
        }
        simpleAdapter = new SimpleAdapter(this, data, R.layout.listview_item, new String[]{"pic","txt"}, new int[]{R.id.item_image, R.id.item_text});
        listView.setAdapter(simpleAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String text = listView.getItemAtPosition(position)+"";
                Toast.makeText(ListViewActivity.this, "position: "+position+"  text: "+text, Toast.LENGTH_SHORT).show();
            }
        });

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState){
                    case SCROLL_STATE_FLING:
                        Log.i("Tag", "用户手指已经离开屏幕，由于惯性，仍在滑动");
                        for (int i=0;i<5;i++){
                            Map<String,Object> map = new HashMap<String, Object>();
                            map.put("pic", R.drawable.toggle_button_off);
                            map.put("txt", "新增项"+i);
                            data.add(map);
                        }
                        simpleAdapter.notifyDataSetChanged();
                        break;
                    case SCROLL_STATE_IDLE:
                        Log.i("Tag", "视图已经停止滑动");
                        break;
                    case SCROLL_STATE_TOUCH_SCROLL:
                        Log.i("Tag", "手指未离开屏幕，正在滑动");
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }
}
